package model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.json.JSONObject;

@Entity
public class Category implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Column
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer idCategory;
	
	@Column
	private String name;
	
	@JoinColumn(name="idCategoryParent", referencedColumnName="idCategory")
	@ManyToOne
	private Category parent;

	@OneToMany(mappedBy="parent")
	private Set<Category> childs;
	
	public Category() {
	}
	
	public Category(Integer idCategory) {
		this.idCategory = idCategory;
	}
	
	@Override
	public String toString() {
		try{
			JSONObject jsonObj = new JSONObject();
			jsonObj.put("id", this.getIdCategory());
			jsonObj.put("idCategory", this.getIdCategory());
			jsonObj.put("name", this.getName());
			return jsonObj.toString();
		}catch (Exception e){
			throw new RuntimeException(e);
		}
	}

	public Integer getIdCategory() {
		return idCategory;
	}

	public void setIdCategory(Integer idCategory) {
		this.idCategory = idCategory;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Category getParent() {
		return parent;
	}

	public void setParent(Category parent) {
		this.parent = parent;
	}

	public void setChilds(Set<Category> childs) {
		this.childs = childs;
	}

	public Set<Category> getChilds() {
		return childs;
	}
	
	
}
